﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssetContentMetaDataMapper
    {
        internal static IMetaData ToBusinessObject(T_AssetContentMetaData entity)
        {
            // parent is causing infinite recursion: obj.AssetContent = AssetContentMapper.ToBusinessObject ( entity.T_AssetContent );
            //if (entity.T_MetaData != null)
                return MetaDataMapper.ToBusinessObject(entity.T_MetaKey,entity.T_MetaValue);
            //else
            //    return null;
            //if (entity.T_Status != null) obj.Status = StatusMapper.ToBusinessObject(entity.T_Status);
        }

        internal static T_AssetContentMetaData ToEntity (int AssetContentId, IMetaData v )
        {
            T_AssetContentMetaData entity = new T_AssetContentMetaData ( );

            
//            entity.AssetContentMetaData_IDPK = v.ID;
            
            entity.AssetContentID_FK = AssetContentId;
            //entity.MetaDataID_FK = v.ID;
            entity.StatusID_FK = 0;

            // parent is causing infinite recursion: entity.T_AssetContent = AssetContentMapper.ToEntity ( v.AssetContent );
            CondeNet.AssetManager.DataProvider.Sql.Mappers.MetaDataMapper.MetadataPairing
                mdKeyValue = MetaDataMapper.ToEntity ( v );

            entity.T_MetaKey = mdKeyValue.MetaKey;
            entity.T_MetaValue = mdKeyValue.MetaValue;
            entity.MetaKeyID_FK = mdKeyValue.MetaKey.MetaKey_IDPK;
            entity.MetaValueID_FK = mdKeyValue.MetaValue.MetaValue_IDPK;
            //if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );

            return entity;
        }

        internal static IList<IMetaData> ToBusinessObjects(IList<T_AssetContentMetaData> dalList)
        {
            return dalList.Select(tmp => ToBusinessObject(tmp)).ToList();
        }

        internal static IList<T_AssetContentMetaData> ToEntities ( int AssetContentId, IList<IMetaData> entityList )
        {
            EntitySet<T_AssetContentMetaData> list = new EntitySet<T_AssetContentMetaData>();
            entityList.Select ( tmp => ToEntity (AssetContentId, tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
